#include "gamepage.h"

#include <QDebug>
#include <QLabel>
#include <QPushButton>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QFile>
#include <QApplication>

MainPage::MainPage(): QWidget(nullptr) {
    setAutoFillBackground(true);
    QPalette palette = this->palette();
    QPixmap pixmap(":/background/main.jpg");
    palette.setBrush(QPalette::Window,
                     QBrush(pixmap)
                     );
    setPalette(palette);
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setAlignment(Qt::AlignHCenter);

    title = new QLabel("飞机大战");
    QFont ft;
    ft.setBold(true);
    ft.setPointSize(32);
    title->setFont(ft);
    layout->addWidget(title, 2);

    start = new QPushButton("开始");
    connect(start, &QPushButton::released, this, [this]{emit startGame();});
    layout->addWidget(start);

    resume = new QPushButton("继续游戏");
    connect(resume, &QPushButton::released, this, [this]{emit resumeGame();});
    layout->addWidget(resume);

    reload();


    help = new QPushButton("玩法介绍");
    connect(help, &QPushButton::released, this, [this]{emit showHelp();});
    layout->addWidget(help);

    exit = new QPushButton("退出游戏");
    connect(exit, &QPushButton::released, this, [this]{emit quitGame();});
    layout->addWidget(exit);

    layout->addStretch(1);

    setLayout(layout);
}
void MainPage::reload() {
    QFile saveFile(QApplication::applicationDirPath().append("/save.dat"));
    resume->setEnabled(saveFile.exists());
}
LevelPage::LevelPage() {
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Window, Qt::black);
    setPalette(pal);

    title = new QLabel("？？？");
    QFont ft;
    ft.setBold(true);
    ft.setPointSize(32);
    title->setFont(ft);
    title->setStyleSheet("color:white;");

    auto *layout = new QHBoxLayout;
    layout->setAlignment(Qt::AlignCenter);
    layout->addWidget(title);
    setLayout(layout);
}
void LevelPage::setLevel(const GameLevel &level) {
    title->setText(level.name);
}
EndPage::EndPage() {
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Window, Qt::black);
    setPalette(pal);

    auto *layout = new QVBoxLayout();
    layout->setAlignment(Qt::AlignCenter);

    status = new QLabel;
    status->resize(300, 200);

    success.load(":/pic/victory.png");
    success.scaled(status->size(), Qt::KeepAspectRatio);
    failed.load(":/pic/dead.jpg");
    failed.scaled(status->size(), Qt::KeepAspectRatio);

    status->setPixmap(success);

    layout->addWidget(status, 2);

    score = new QLabel("？？？？");
    QFont ft;
    ft.setBold(true);
    ft.setPointSize(32);
    score->setFont(ft);
    score->setStyleSheet("color:white;");

    layout->addWidget(score, 1);

    restart = new QPushButton("重新开始");
    connect(restart, &QPushButton::released, this, [this]{emit restartGame();});
    layout->addWidget(restart);

    back = new QPushButton("返回菜单");
    connect(back, &QPushButton::released, this, [this]{emit backToMain();});
    layout->addWidget(back);

    quit = new QPushButton("退出游戏");
    connect(quit, &QPushButton::released, this, [this]{emit quitGame();});

    layout->addWidget(quit);

    layout->addStretch(1);

    setLayout(layout);
}
void EndPage::setState(bool isSuccess, int score) {
    if(isSuccess){
        status->setPixmap(success);
    } else{
        status->setPixmap(failed);
    }
    this->score->setText(QString("分数：%1").arg(score));
}

PausePage::PausePage() {
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Window, Qt::black);
    setPalette(pal);

    auto *layout = new QVBoxLayout();
    layout->setAlignment(Qt::AlignCenter);

    title = new QLabel("暂停");
    QFont ft;
    ft.setBold(true);
    ft.setPointSize(32);
    title->setFont(ft);
    title->setStyleSheet("color:white;");
    layout->addWidget(title, 2);

    resume = new QPushButton("继续游戏");
    connect(resume, &QPushButton::released, this, [this]{emit resumeGame();});
    layout->addWidget(resume);

    saveAndQuit = new QPushButton("保存&退出");
    connect(saveAndQuit, &QPushButton::released, this, [this]{
        emit saveGame();
        emit endGame();
    });
    layout->addWidget(saveAndQuit);

    quit = new QPushButton("退出游戏");
    connect(quit, &QPushButton::released, this, [this]{emit endGame();});
    layout->addWidget(quit);

    layout->addStretch(1);

    setLayout(layout);
}
HelpPage::HelpPage() {
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Window, Qt::black);
    setPalette(pal);

    auto *layout = new QVBoxLayout();
    layout->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);

    title = new QLabel("帮助");
    QFont ft;
    ft.setBold(true);
    ft.setPointSize(32);
    title->setFont(ft);
    title->setStyleSheet("color:white;");
    layout->addWidget(title, 2);

    ft.setPointSize(18);
    QLabel *label;
    label = new QLabel("WASD 控制移动");
    label->setFont(ft);
    label->setStyleSheet("color:white;");
    layout->addWidget(label);
    label = new QLabel("1、2、3、4 切换子弹");
    label->setFont(ft);
    label->setStyleSheet("color:white;");
    layout->addWidget(label);
    label = new QLabel("ESC 显示暂停菜单");
    label->setFont(ft);
    label->setStyleSheet("color:white;");
    layout->addWidget(label);
    layout->addStretch(1);
    back = new QPushButton("返回");
    connect(back, &QPushButton::released, this, [this]{emit backToMain(); });
    layout->addWidget(back);

    layout->addStretch(1);
    setLayout(layout);
}
DifficultChoicePage::DifficultChoicePage() {
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Window, Qt::black);
    setPalette(pal);

    auto *layout = new QVBoxLayout();
    layout->setAlignment(Qt::AlignCenter);

    title = new QLabel("选择难度");
    QFont ft;
    ft.setBold(true);
    ft.setPointSize(32);
    title->setFont(ft);
    title->setStyleSheet("color:white;");
    layout->addWidget(title, 2);

    easy = new QPushButton("简单");
    connect(easy, &QPushButton::released, this, [this]{emit selectDifficult(EASY);});
    layout->addWidget(easy);

    normal = new QPushButton("正常");
    connect(normal, &QPushButton::released, this, [this]{emit selectDifficult(NORMAL);});
    layout->addWidget(normal);

    hard = new QPushButton("困难");
    connect(hard, &QPushButton::released, this, [this]{emit selectDifficult(HARD);});
    layout->addWidget(hard);

    layout->addStretch(1);

    setLayout(layout);
}
